package com.turing.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turing.common.util.Tools;
import com.turing.user.dao.RoleMapper;
import com.turing.user.dao.RoleUserMapper;
import com.turing.user.entity.Role;
import com.turing.user.entity.RoleUser;
import com.turing.user.entity.User;
import com.turing.user.service.RoleUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Description 服务类实现
* @Author livin
* @Date 2022-02-22
*/
@Service
public class RoleUserServiceImpl extends ServiceImpl<RoleUserMapper, RoleUser> implements RoleUserService {

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Override
    public List<RoleUser> getUserRoles(Integer userId) {

        return new LambdaQueryChainWrapper<>(roleUserMapper).eq(RoleUser::getUserId,userId).orderByAsc(RoleUser::getCreatedTime).list();

    }

    @Override
    public List<Integer> getUserIdsByRoleId(Integer mainRoleId) {
        List<RoleUser> list = new LambdaQueryChainWrapper<>(baseMapper).eq(RoleUser::getRoleId, mainRoleId).list();
        if(Tools.isNotEmpty(list)){
            return list.stream().map(RoleUser::getUserId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<Integer> getRoleIdsByUserId(Integer userId) {
        List<RoleUser> roleUsers = new LambdaQueryChainWrapper<>(baseMapper).eq(RoleUser::getUserId, userId)
                .orderByAsc(RoleUser::getCreatedTime).list();
        if (Tools.isNotEmpty(roleUsers)) {
            return roleUsers.stream().map(RoleUser::getRoleId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public boolean getRoleIdByUserId(Integer userId) {
        List<RoleUser> list = new LambdaQueryChainWrapper<>(baseMapper).eq(RoleUser::getUserId,userId).eq(RoleUser::getRoleId,293).list();
        return Tools.isEmpty(list);
    }

}
